/*
 * DrawableFactory.java
 *
 * Copyright(c) 2011
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.colorschemes;

import android.graphics.ColorFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;

public class DrawableFactory {

	private final static float Xr = 7.0f;
	private final static float Yr = 7.0f;
	private final static float Xf = 0.0f;
	private final static float Yf = 0.0f;

	protected enum RoundCorner {
		TOP, BOTTOM, BOTH, NO
	};

	private final static float[] topRoundCorner = { Xr, Yr, Xr, Yr, Xf, Yf, Xf, Yf };
	private final static float[] bottomRoundCorner = { Xf, Yf, Xf, Yf, Xr, Yr, Xr, Yr };
	private final static float[] bothRoundCorner = { Xr, Yr, Xr, Yr, Xr, Yr, Xr, Yr };
	private final static float[] noRoundCorner = { Xf, Yf, Xf, Yf, Xf, Yf, Xf, Yf };

	private final static int strokeWidth = 1;

	private final static int[] pressed = new int[] { android.R.attr.state_pressed,
			-android.R.attr.state_selected };
	private final static int[] selected = new int[] { android.R.attr.state_selected };
	private final static int[] normal = new int[] { android.R.attr.state_enabled };
	
	protected enum GradientColor {
		LIGHT_TO_DARK, DARK_TO_LIGHT
	};

	protected enum GradientWay {
		ONE_TWO_ONE, ONE_TWO, TWO_ONE
	}

	class ColorCollection {
		public int colorLight;
		public int colorDark;
		public int strokeColor;
		public int textColor;

		public ColorCollection(int colorLight, int colorDark, int strokeColor,
				int textColor) {
			this.colorLight = colorLight;
			this.colorDark = colorDark;
			this.strokeColor = strokeColor;
			this.textColor = textColor;
		}
	}

	public Drawable createGradientDrawable(GradientWay gw, GradientColor gc,
			ColorCollection cc, RoundCorner rc) {
		int gradientColorOne, gradientColorTwo;
		if (gc == GradientColor.LIGHT_TO_DARK) {
			gradientColorOne = cc.colorLight;
			gradientColorTwo = cc.colorDark;
		} else {
			gradientColorOne = cc.colorDark;
			gradientColorTwo = cc.colorLight;
		}

		int[] gradientColor;
		if (gw == GradientWay.ONE_TWO) {
			gradientColor = new int[] { gradientColorOne, gradientColorTwo };
		} else if (gw == GradientWay.TWO_ONE) {
			gradientColor = new int[] { gradientColorTwo, gradientColorOne };
		} else {
			gradientColor = new int[] { gradientColorOne, gradientColorTwo,
					gradientColorOne };
		}

		GradientDrawable gd = new GradientDrawable(
				GradientDrawable.Orientation.LEFT_RIGHT, gradientColor);
		gd.setGradientType(GradientDrawable.LINEAR_GRADIENT);
		createRoundedCorner(gd, rc);

		createStroke(gd, cc);

		return gd;
	}

	public void createRoundedCorner(GradientDrawable gd, RoundCorner rc) {
		float[] corner;
		if (rc == RoundCorner.TOP)
			corner = topRoundCorner;
		else if (rc == RoundCorner.BOTTOM)
			corner = bottomRoundCorner;
		else if (rc == RoundCorner.BOTH)
			corner = bothRoundCorner;
		else
			corner = noRoundCorner;

		gd.setCornerRadius(1.0f);
		gd.setCornerRadii(corner);
	}

	public void createStroke(GradientDrawable gd, ColorCollection cc) {
		gd.setStroke(strokeWidth, cc.strokeColor);
	}

	public void createFiltered(Drawable d, int color) {
		ColorFilter cf = new PorterDuffColorFilter(color,
				PorterDuff.Mode.MULTIPLY);
		d.setColorFilter(cf);
	}

	class StateDrawable {
		Drawable drawable;
		int state;
	}

	public StateListDrawable createSelectedUnSelectedDrawable(
			Drawable normalDrawable, Drawable selectedDrawable) {
		
		StateListDrawable sld = new StateListDrawable();

		sld.addState(pressed, selectedDrawable);
		sld.addState(selected, selectedDrawable);
		sld.addState(normal, normalDrawable);

		return sld;
	}
}
